package com.wfs.suanfa.nio.demo;

import java.nio.Buffer;
import java.nio.CharBuffer;

public class CharBufferTest {
    public static void main(String[] args) {
        CharBuffer buff = CharBuffer.allocate(8);
        System.out.println("capacity:" + buff.capacity());
        System.out.println("limit:" + buff.limit());
        System.out.println("position:" + buff.position());
        buff.put('a');
        buff.put('b');
        buff.put('c');
        System.out.println("加入三个元素后,position=" + buff.position());
        System.out.println("capacity:" + buff.capacity());
        //将buff从写模式切换为读模式
        buff.flip();
        System.out.println("执行flip后,limit=" + buff.limit());
        System.out.println("position=" + buff.position());

        Buffer mark = buff.mark();
        System.out.println(mark);
        //取出第一个元素
        System.out.println("第一个元素(position=0):" + buff.get());
        System.out.println("取出第一个元素后,position=" + buff.position());
        Buffer reset = mark.reset();
        buff.clear();
        System.out.println("执行clear方法后,limit=" + buff.limit());
        System.out.println("执行clear方法后,position=" + buff.position());
        System.out.println("执行clear后,缓冲区的内容并没有被清空.第三个元素为:" + buff.get(2));
        System.out.println("执行绝对读取后,position=" + buff.position());
        
        
        /*try {
            RandomAccessFile randomAccessFile=new RandomAccessFile("a.txt","rw");
            FileChannel channel = randomAccessFile.getChannel();
            ByteBuffer buf=ByteBuffer.allocate(1);
            //buf.put(Byte.parseByte("123"));
            int read=channel.read(buf);
            while (read!=-1){
                buf.flip();

                while (buf.hasRemaining()){
                    char aa=(char)buf.get();
                    if (aa==(char)'v'){
                        buf.mark();
                    }
                    System.out.print(aa);
                    if (aa==(char)'a'){
                        buf.reset();
                    }
                }

                buf.compact();
                read=channel.read(buf);
            }
            randomAccessFile.close();
        } catch (Exception e) {
            e.printStackTrace();
        }*/

    }
}
